/**
 * Copyright (C) 2009 SC 4ViewSoft SRL
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package math.spbu.ru.historycharts.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import math.spbu.ru.historycharts.ChartsView;
import math.spbu.ru.historycharts.util.MathHelper;
import android.util.Log;

/**
 * An XY series encapsulates values for XY charts like line, time, area,
 * scatter... charts.
 */
public class XYSeries implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * The series title.
	 */
	private String mTitle;
	/**
     * 
     */
	private List<DatePoint> points = new ArrayList<DatePoint>();

	private float[] pointsVec;

	private double mMinX = MathHelper.NULL_VALUE;
	/**
	 * The maximum value for the X axis.
	 */
	private double mMaxX = -MathHelper.NULL_VALUE;
	/**
	 * The minimum value for the Y axis.
	 */
	private double mMinY = MathHelper.NULL_VALUE;
	/**
	 * The maximum value for the Y axis.
	 */
	private double mMaxY = -MathHelper.NULL_VALUE;

	private int firstInRange = 0, lastInRange = 0;

	/**
	 * Builds a new XY series.
	 * 
	 * @param title
	 *            the series title.
	 */
	public XYSeries(String title) {
		mTitle = title;
		firstInRange = 0;
		lastInRange = 0;
		initRange();
	}

	/**
	 * Initializes the range for both axes.
	 */
	private void initRange() {
		mMinX = MathHelper.NULL_VALUE;
		mMaxX = -MathHelper.NULL_VALUE;
		mMinY = MathHelper.NULL_VALUE;
		mMaxY = -MathHelper.NULL_VALUE;

		if (points.size() != 0) {
			updateFirstLast();
			for (int k = firstInRange; k <= lastInRange; k++)
				updateRange(points.get(k));
		}
	}

	void initRangeY() {
		mMinY = MathHelper.NULL_VALUE;
		mMaxY = -MathHelper.NULL_VALUE;

		if (points.size() != 0) {
			for (int k = firstInRange; k <= lastInRange; k++)
				updateRangeY(points.get(k));
		}
	}

	/**
	 * Updates the range on both axes.
	 * 
	 * @param x
	 *            the new x value
	 * @param y
	 *            the new y value
	 */
	private void updateRange(DatePoint point) {
		mMinX = Math.min(mMinX, point.getDate().getTimeInMillis());
		mMaxX = Math.max(mMaxX, point.getDate().getTimeInMillis());
		mMinY = Math.min(mMinY, point.getValue());
		mMaxY = Math.max(mMaxY, point.getValue());
	}

	private void updateRangeY(DatePoint point) {
		mMinY = Math.min(mMinY, point.getValue());
		mMaxY = Math.max(mMaxY, point.getValue());
	}

	public void updateFirstLast() {
		firstInRange = getFirstInRange();
		lastInRange = getLastInRange();
	}

	/**
	 * Returns the series title.
	 * 
	 * @return the series title
	 */
	public String getTitle() {
		return mTitle;
	}

	/**
	 * Sets the series title.
	 * 
	 * @param title
	 *            the series title
	 */
	public void setTitle(String title) {
		mTitle = title;
	}

	/**
	 * Adds a new value to the series.
	 * 
	 * @param x
	 *            the value for the X axis
	 * @param y
	 *            the value for the Y axis
	 */
	public void add(DatePoint datePoint) {
		points.add(datePoint);
		updateRange(datePoint);
	}

	/**
	 * Removes an existing value from the series.
	 * 
	 * @param index
	 *            the index in the series of the value to remove
	 */
	public void remove(int index) {
		boolean isDel = points.remove(index) != null;
		if (isDel) {
			initRange();
		}
	}

	/**
	 * Removes all the existing values from the series.
	 */
	public void clear() {
		points.clear();
		initRange();
	}

	/**
	 * Returns the X axis value at the specified index.
	 * 
	 * @param index
	 *            the index
	 * @return the X value
	 */
	public Calendar getX(int index) {
		return points.get(index).getDate();
	}

	/**
	 * Returns the Y axis value at the specified index.
	 * 
	 * @param index
	 *            the index
	 * @return the Y value
	 */
	public float getY(int index) {
		return points.get(index).getValue();
	}
	
	public DatePoint get(int index) {
		return points.get(index);
	}

	/**
	 * Returns the series item count.
	 * 
	 * @return the series item count
	 */
	public int getItemCount() {
		return points.size();
	}

	public int getItemCountInRange() {
		return lastInRange - firstInRange;
	}

	private int getFirstInRange() {

		if (ChartsView.getDataProvider() == null)
			return 0;

		int i = 0;
		double min = ChartsView.getDataProvider().getRenderer().getXAxisMin();
		while (points.get(i).getDate().getTimeInMillis() < min)
			i++;
		return i > 0 ? i - 1 : 0;
	}

	public int getFirst() {
		return firstInRange;
	}

	private int getLastInRange() {
		if (ChartsView.getDataProvider() == null)
			return points.size() - 1;

		int i = firstInRange;
		Log.d("BNV", "firstInRange" + firstInRange + " size " + points.size());
		double max = ChartsView.getDataProvider().getRenderer().getXAxisMax();
		while (i < points.size()
				&& points.get(i).getDate().getTimeInMillis() < max)
			i++;
		Log.d("BNV", "getLastInRange" + (i < points.size() ? i : i - 1) + " size " + points.size());
		return i < points.size() ? i : i - 1;
	}

	public int getLast() {
		return lastInRange;
	}

	/**
	 * Returns the minimum value on the X axis.
	 * 
	 * @return the X axis minimum value
	 */
	public double getMinX() {
		return mMinX;
	}

	/**
	 * Returns the minimum value on the Y axis.
	 * 
	 * @return the Y axis minimum value
	 */
	public double getMinY() {
		return mMinY;
	}

	/**
	 * Returns the maximum value on the X axis.
	 * 
	 * @return the X axis maximum value
	 */
	public double getMaxX() {
		return mMaxX;
	}

	/**
	 * Returns the maximum value on the Y axis.
	 * 
	 * @return the Y axis maximum value
	 */
	public double getMaxY() {
		return mMaxY;
	}

	public void addListInEnd(List<DatePoint> list) {
		points.addAll(list);
		initRange();
	}

	public void addListInStart(List<DatePoint> list) {
		points.addAll(0, list);
		initRange();
	}

	public void setPointsVector(float[] points) {
		pointsVec = points;
	}

	public float[] getPointsVector() {
		return pointsVec;
	}
	/*
	 * public long getMinXValue(){ return
	 * points.get(0).getDate().getTimeInMillis(); }
	 * 
	 * public long getMaxXValue(){ return
	 * points.get(points.size()-1).getDate().getTimeInMillis(); }
	 */

}
